<?php

/**
 * @author guoxiaoyong
 */
namespace frontend\controllers;

use bc\model\AddressModel;
use bc\service\DictCityService;
use bc\service\DictDistrictService;
use bc\service\DictProvinceService;
use yii\web\Session;
use bc\controller\WxOAuthController;

class AddressController extends WxOAuthController
{
	public function actionIndex()
	{
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		
		$model = new AddressModel ();
		
		$params['uid'] = $user['user_id'];
		$params['is_invalid'] = 1;
		
		$page = $this->get ( 'page', 1 );
		$list = $model->getList ( $params, 1, 15 );
		
		$view ['list'] = $list;
		
		return $this->render ( 'index', $view );
	}
	
	
	/**
	 * 选择收货地址列表
	 */
	public function actionList()
	{
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		$model = new AddressModel ();
		$params['uid'] = $user['user_id'];
		$params['is_invalid'] = 1;
		
		$page = $this->get ( 'page', 1 );
		$list = $model->getList ( $params, 1, 15 );
		
		$view ['list'] = $list;
		
		return $this->render('list', $view);
	}

	/**
	 * 获取省
	 */
	public function actionAjaxprovince()
	{

		$provinceService = new DictProvinceService();
		$provinceService->columns = 'id,province_name';
		$provinceService->sort = 'id desc';
		$provinceList = $provinceService->getList ( );
		$result = array (
			'msg' => '获取成功',
			'result' => 1,
			'data' => $provinceList
		);
		echo \json_encode ( $result );
	}

	/**
	 * 根据省获取城市
	 */
	public function actionAjaxcitysbyprovincesid()
	{
		$provincesid = $this->post ( 'value' );
		
		$where = array (
				'province_id' => $provincesid 
		);
		
		$cityService = new DictCityService ();
		$cityService->columns = 'id,city_name';
		$cityService->sort = 'id desc';
		$cityList = $cityService->getList ( $where );
		$result = array (
				'msg' => '获取成功',
				'result' => 1,
				'data' => $cityList 
		);
		echo \json_encode ( $result );
	}
	
	/**
	 * 根据城市获取地区
	 */
	public function actionAjaxareasbycityid()
	{
		$cityid = $this->post ( 'value' );
		$where = array (
				'city_id' => $cityid 
		);
		$districtService = new DictDistrictService ();
		$districtService->columns = 'id,district_name';
		$districtService->sort = 'id desc';
		$districtList = $districtService->getList ( $where );
		$result = array (
				'msg' => '获取成功',
				'result' => 1,
				'data' => $districtList 
		);
		echo \json_encode ( $result );
	}
	
	/**
	 * 新增收货地址
	 */
	public function actionAdd()
	{
		// 获取省
		$provinceService = new DictProvinceService ();
		$provinceService->sort = 'id ASC';
		$provinceList = $provinceService->getList ();
		
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		
		$model = new AddressModel();
		// 计算是否有默认收货地址
		$count = $model->getTotal ( [
				'uid' => $user ['user_id'],
				'is_invalid' => 1,
				'default_address' => 1
		] );
		$view ['count'] = $count;
		
		$view ['province'] = $provinceList;
		return $this->render ( 'add', $view );
	}
	
	
	/**
	 * 编辑
	 */
	public function actionEdit()
	{
		// 获取省
		$provinceService = new DictProvinceService ();
		$provinceService->sort = 'id ASC';
		$provinceList = $provinceService->getList ();
		
		
		$id = $this->get('id');
		$addressModel = new AddressModel();
		$row = $addressModel->getRowById($id);
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		
		if($row['uid'] != $user['user_id'] || $row['is_invalid'] == 0)
			$this->redirect('/');
		
		$view ['row'] = $row;
		$view ['province'] = $provinceList;
		return $this->render ( 'edit', $view );
	}
	
	/**
	 * 更新数据
	 */
	public function actionSave()
	{
		$session = new Session ();
		$user = $session->get ( 'shop_user' );
		
		if (! $user)
			$this->redirect ( '/' );
		
		$id = $this->post ( 'id' );
		
		// 表单保存
		if ($this->getRequest ()->isPost == true)
		{
			$model = new AddressModel ();
			$data ['uid'] = $user ['user_id'];
			// 收货人
			$data ['addressee'] = $this->post ( 'addressee' );
			// 收货人地址
			$data ['telephone'] = $this->post ( 'telephone' );
			// 邮编
			$data ['zip_code'] = $this->post ( 'zip_code' );
			
			if ($this->post ( 'province_id' ))
			{
				$provinceService = new DictProvinceService ();
				$data ['province_id'] = $this->post ( 'province_id' );
				$province = $provinceService->getRow ( [ 
						'id' => $data ['province_id'] 
				] );
				$data ['province'] = $province ['province_name'];
			}
			
			if ($this->post ( 'city_id' ))
			{
				$cityService = new DictCityService ();
				$data ['city_id'] = $this->post ( 'city_id' );
				$city = $cityService->getRow ( [ 
						'id' => $data ['city_id'] 
				] );
				$data ['city'] = $city ['city_name'];
			}
			
			if ($this->post ( 'district_id' ))
			{
				$districtService = new DictDistrictService ();
				$data ['district_id'] = $this->post ( 'district_id' );
				$district = $districtService->getRow ( [ 
						'id' => $data ['district_id'] 
				] );
				$data ['district'] = $district ['district_name'];
			}
			
			$data ['address'] = $this->post ( 'address' );
			
			$data ['default_address'] = $this->post ( 'is_default' , 0);
			
			$addressModel = new AddressModel();
			$row = $addressModel->getRow(['uid' => $user['user_id'], 'is_invalid' => 1]);
			
			//修改的收货地址为默认地址的时候
			if($data ['default_address'] == 1)
			{
				$addressModel->updateAll(['default_address' => 0], ['uid' => $user['user_id'], 'is_invalid' => 1]);
				if($id)
					$addressModel->modify(['default_address' => 1], $id);
			}
			
			//如果没有收货地址 第一个收货地址为默认收货地址
			if(!$row)
				$data ['default_address'] = 1;
			
			$data ['is_invalid'] = 1;
			$data ['upate_time'] = time ();
			
			if ($id)
			{
				$data ['create_time'] = time ();
				$model->modify ( $data, $id );
			}
			else
			{
				$model->addAdd ( $data );
			}
		}
		
		$this->redirect('/address');
	}
	
	/**
	 * 删除收货地址
	 */
	public function actionDelete()
	{
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		$id = $this->get ( 'id' );
		
		$model = new AddressModel ();
		
		$deleteRow = $model->getRowById($id);
		
		//remove address
		$model->modify ( [ 
				'uid' => $shop_user ['user_id'],
				'is_invalid' => 0,
				'default_address' => 0
		], $id );
		
		//删除的地址为默认地址
		if($deleteRow['default_address'] == 1)
		{	
			$addressList = $model->getList( [
					'uid' => $shop_user ['user_id'],
					'is_invalid' => 1
			] );
			if ($addressList)
			{
				$model->modify ( [
						'default_address' => 1
				], $addressList[0]['id'] );
			}
		}
		
		$this->redirect ( '/address' );
	}
	
	
	/**
	 * 设置默认收货地址
	 */
	public function actionDefault()
	{
		$id = $this->get('id');
		$session = new Session ();
		$shop_user = $session->get ( 'shop_user' );
		$model = new AddressModel ();
		$model->updateAll(['default_address' => 0], ['uid' => $shop_user['user_id'], 'is_invalid' => 1]);
		$model->modify(['default_address' => 1], $id);
		$msg['error'] = 0;
		$msg['msg'] = '修改成功';
		echo json_encode($msg);
		exit;
	}
	
	
	/**
	 * 选择地址
	 */
	public function actionChoose()
	{
		$id = $this->get('id');
		$this->redirect('/sale/order/quick?address_id='.$id);
	}
}